/*
 * @(#) AjaxBaseAction.java 2010-9-6
 *
 * Copyright (c) 2010, Strong Technology. All Rights Reserved.
 * Strong Technology. CONFIDENTIAL
 */
package cn.strong.gweva.common.presentation.action;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import cn.strong.gweva.common.business.entity.ErrorEntity;

import com.googlecode.jsonplugin.JSONUtil;


/**
 * AjaxAction基类
 * 
 * @author lilanfei
 * @version 1.0
 * @since 2010-9-04
 */
public abstract class AjaxBaseAction extends PagerableAction implements ServletRequestAware,
		ServletResponseAware
{

	/**
	 * <code>serialVersionUID</code>
	 */
	private static final long serialVersionUID = -4964325419184481218L;
	/**
	 * 请求
	 */
	protected HttpServletRequest request;
	/**
	 * 返回
	 */
	protected HttpServletResponse response;
	/**
	 * session
	 */
	protected HttpSession session;
	/**
	 * 错误信息
	 */
	private ErrorEntity errMessage;
	/**
	 * 错误的控件
	 */
	private String focusId = "";
	/**
	 * 开始页
	 */
	protected int start;
	/**
	 * 结束页
	 */
	protected int limit;

	/**
	 * 
	 */
	public void setServletRequest(HttpServletRequest req)
	{
		this.request = req;
		this.session = request.getSession();
	}

	/**
	 * 
	 */
	public void setServletResponse(HttpServletResponse res)
	{
		this.response = res;
	}
	/**
	 * 写错误
	 */
	public void writeError()
	{
		writeJson(errMessage);
	}

	/**
	 * 写JSON数据结构
	 * @param obj
	 */
	public void writeJson(Object obj)
	{
		try
		{
			// 格式
			response.setContentType("text/html;charset=utf-8");
			PrintWriter writer = response.getWriter();
			// 写出JSON数据结构
			writer.write(JSONUtil.serialize(obj));
			writer.flush();
			writer.close();
		}
		catch (Exception ex)
		{
		}
	}

	/**
	 * 写JSON数据结构
	 * @param obj
	 */
	public void writeJson(Object obj, boolean ignoreHieraychy, boolean excludeNullProperties)
	{
		try
		{
			// 格式
			response.setContentType("text/html;charset=utf-8");
			PrintWriter writer = response.getWriter();
			// 写出JSON数据结构
			writer.write(JSONUtil.serialize(obj, null, null, ignoreHieraychy, excludeNullProperties));
			writer.flush();
			writer.close();
		}
		catch (Exception ex)
		{
		}
	}

	/**
	 * 写JSON数据结构 带错误信息的
	 * @param obj 数据
	 * @param isSuccess true正常  false:出错
	 */
	public void writeJson(Object obj, boolean isSuccess)
	{
		try
		{
			response.setContentType("text/html;charset=utf-8");
			PrintWriter writer = response.getWriter();
			if (isSuccess)
			{
				writer.write("{success:true,data:");
			}
			else
			{
				writer.write("{success:false,errors:true,data:");
			}
			writer.write(JSONUtil.serialize(obj, null, null, false, false));
			writer.write("}");
			writer.flush();
			writer.close();
		}
		catch (Exception ex)
		{
		}
	}

	protected void addFocus(String id)
	{
		focusId = id;
	}

	/**
	 * 判断是否错误
	 * @return true:有错误 false:没有错误
	 */
	public boolean isInvalid()
	{
		if (errMessage != null)
		{
			writeError();
			return true;
		}
		return false;
	}

	/**
	 * @return request
	 */
	public HttpServletRequest getRequest()
	{
		return request;
	}

	/**
	 * @param request request
	 */
	public void setRequest(HttpServletRequest request)
	{
		this.request = request;
	}

	/**
	 * @return response
	 */
	public HttpServletResponse getResponse()
	{
		return response;
	}

	/**
	 * @param response response
	 */
	public void setResponse(HttpServletResponse response)
	{
		this.response = response;
	}

	/**
	 * @return session
	 */
	public HttpSession getSession()
	{
		return session;
	}

	/**
	 * @param session session
	 */
	public void setSession(HttpSession session)
	{
		this.session = session;
	}

	/**
	 * @return errMessage
	 */
	public ErrorEntity getErrMessage()
	{
		return errMessage;
	}

	/**
	 * @param errMessage errMessage
	 */
	public void setErrMessage(ErrorEntity errMessage)
	{
		this.errMessage = errMessage;
	}

	/**
	 * @return focusId
	 */
	public String getFocusId()
	{
		return focusId;
	}

	/**
	 * @param focusId focusId
	 */
	public void setFocusId(String focusId)
	{
		this.focusId = focusId;
	}

	/**
	 * @return start
	 */
	public int getStart()
	{
		return start;
	}

	/**
	 * @param start start
	 */
	public void setStart(int start)
	{
		this.start = start;
	}

	/**
	 * @return limit
	 */
	public int getLimit()
	{
		return limit;
	}

	/**
	 * @param limit limit
	 */
	public void setLimit(int limit)
	{
		this.limit = limit;
	}

	@Override
	public void setPageRows(String pageRows)
	{
		
	}

	@Override
	public void setTotalCount(String totalCount)
	{
		
	}

}
